/**
 * 
 */
package com.vision.core.cl.ui;

import java.util.Arrays;

import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.widget.core.client.grid.filters.Filter;
import com.sencha.gxt.widget.core.client.grid.filters.ListFilter;

/**
 * Convenience class for creating enum-based {@link Filter}s.
 *
 * @see Filter
 * 
 * @author Mark
 *
 */
public class EnumFilter<T, E extends Enum<E>> extends ListFilter<T, E> {

	/**
	 * Creates an instance of the filter based on <tt>valueProvider</tt> and
	 * <tt>values</tt> (array of enum values).
	 * 
	 * @param valueProvider
	 * @param values
	 */
	public EnumFilter(ValueProvider<? super T, E> valueProvider, E[] values) {
		this(valueProvider, createStore(values));
	}
	
	public EnumFilter(ValueProvider<? super T, E> valueProvider, ListStore<E> store) {
		super(valueProvider, store);
	}
	
	private static <E extends Enum<E>> ListStore<E> createStore(E[] values) {
		ModelKeyProvider<E> keyProvider = new ModelKeyProvider<E>() {
			@Override
			public String getKey(E item) {
				return item.name();
			}
		};
		ListStore<E> store = new ListStore<E>(keyProvider);
		store.addAll( Arrays.asList(values) );
		return store;
	}

}
